package com.bjdd.jdy.ui.resetPwd;

import android.os.Bundle;
import android.os.CountDownTimer;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.bjdd.jdy.R;
import com.bjdd.jdy.base.BaseActivity;
import com.bjdd.jdy.util.SpUtil;
import com.bjdd.jdy.util.StatusBarUtil;
import com.bjdd.jdy.util.T;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ResetPwdActivity extends BaseActivity<ResetPwdPresenter, ResetPwdModel> implements ResetPwdContract.View {


    @Bind(R.id.toolbar_back)
    ImageView toolbarBack;
    @Bind(R.id.title)
    TextView title;
    @Bind(R.id.et_reset_mobile)
    EditText etResetMobile;
    @Bind(R.id.et_reset_code)
    EditText etResetCode;
    @Bind(R.id.btn_reset_getcode)
    Button btnResetGetcode;
    @Bind(R.id.et_reset_pwd)
    EditText etResetPwd;
    @Bind(R.id.tv_toolbar_right)
    TextView commit;
    @Bind(R.id.tv_reset_prompt)
    TextView prompt;
    private CountDownTimer timer;

    @Override
    public int getLayoutId() {
        return R.layout.activity_reset_pwd;
    }

    @Override
    public void initView() {
        StatusBarUtil.setColor(this,getResources().getColor(R.color.toolbar_bg),0);
        title.setText("重设密码");
        commit.setText("保存");
        commit.setVisibility(View.VISIBLE);
        String str = "温馨提示:为了验证您的身份，需向";
        String phone = SpUtil.getUser().getMobile().replaceAll("(\\d{3})\\d{4}(\\d{4})","$1****$2");
        str += phone;
        str += "发送一条验证码，请点击获取按钮后输入验证码。";
        prompt.setText(str);
    }

    @Override
    public void resetSuccess() {
        finish();
    }

    @Override
    protected void onDestroy() {
        if (timer!=null){
            timer.onFinish();
            timer.cancel();
            timer = null;
        }
        super.onDestroy();
    }

    @OnClick({R.id.toolbar_back, R.id.btn_reset_getcode, R.id.tv_toolbar_right})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.toolbar_back:
                finish();
                break;
            case R.id.btn_reset_getcode:
                String mobile = SpUtil.getUser().getMobile();
                if (TextUtils.isEmpty(mobile) || TextUtils.equals(mobile,"")){
                    T.show("请先输入手机号码");
                    return;
                }
                long timeAll = 60*1000;
                long timeSpace = 1000;
                btnResetGetcode.setClickable(false);
                btnResetGetcode.setBackground(getResources().getDrawable(R.drawable.unclickable_rounded));
                presenter.getCode(mobile);
                timer = new CountDownTimer(timeAll,timeSpace) {
                    @Override
                    public void onTick(long millisUntilFinished) {
                        btnResetGetcode.setText("("+millisUntilFinished/timeSpace+")重新获取");
                    }

                    @Override
                    public void onFinish() {
                        btnResetGetcode.setClickable(true);
                        btnResetGetcode.setText("发送验证码");
                        btnResetGetcode.setBackground(getResources().getDrawable(R.color.primary));
                    }
                };
                timer.start();
                break;
            case R.id.tv_toolbar_right:
                String code = etResetCode.getText().toString();
                String pwd = etResetPwd.getText().toString();
                String pwd1 = etResetMobile.getText().toString();
                if (TextUtils.isEmpty(code) || TextUtils.equals(code,"")){
                    T.show("请先输入验证码");
                }else if (TextUtils.isEmpty(pwd) || TextUtils.equals(pwd,"")){
                    T.show("请输入密码");
                }else if(TextUtils.isEmpty(pwd1) || TextUtils.equals(pwd1,"")){
                    T.show("请确认密码");
                }else if (!TextUtils.equals(pwd,pwd1)){
                    T.show("两次输入的密码不同");
                }else {
                    presenter.commit(code,SpUtil.getUser().getMobile(),pwd);
                }
                break;
        }
    }
}
